package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.model.Project;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.ProjectImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/crebas/project")
@RequiredArgsConstructor
public class ProjectResource {
    private final ProjectImpl projectService;
    // 创建一个新的项目
    @PostMapping("/create")
    public ResponseEntity<Response> createProject(@RequestBody Project project) {

        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newProject",projectService.create(project)))
                        .message("新建项目")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );

    }

    // 删除一个项目
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Response> deleteProject(@PathVariable String id) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("deleteResult",projectService.delete(id)))
                        .message("删除项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 更新一个项目
    @PostMapping("/update")
    public ResponseEntity<Response> updateProject(@RequestBody Project project) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedProject",projectService.update(project)))
                        .message("更新项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 查询一个项目
    @GetMapping("/search/{id}")
    public ResponseEntity<Response> searchProject(@PathVariable String id) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("projects",projectService.search(id)))
                        .message("查询id为"+id+"的项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有项目
    @GetMapping("/list/{limit}")
    public ResponseEntity<Response> listProjects(@PathVariable int limit) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("projects",projectService.list(limit)))
                        .message("获取"+limit+"个项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有项目
    @GetMapping("/listAll")
    public ResponseEntity<Response> listAll() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("projects",projectService.listAll()))
                        .message("获取所有项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有位置
    @GetMapping("/listAllPlace")
    public ResponseEntity<Response> listAllPlace() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("projects",projectService.getAllPlace()))
                        .message("获取所有位置")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 查询符合条件的项目
    @PostMapping("/filterProject")
    public ResponseEntity<Response> filterProject(@RequestBody Project project) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedProject",projectService.filterProject(project)))
                        .message("筛选项目")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 导入excel
    @PostMapping("/importExcel")
    public ResponseEntity<String> importExcel(@RequestBody List<Map<String,String>> projects) {
        // 返回文件作为响应
        return ResponseEntity.ok()
                .body(projectService.importExcel(projects));
    }
}
